import {useStore} from "vuex";
import {getApplyNowList,getApplyNowListExclName,DownApplyNowListExcl} from "../../api/apply"
import {onMounted,reactive,ref} from "vue"

interface responeList {
		CLASS_ID: number
		rowid: number
		剩余人数: number
		已缴费人数: number
		最低人数: number
		最大招生人数: number
		状态: string
		班级名称: string
	}
export function useapplynow(){
	const {state} = useStore()
	const DAT = ref<responeList[]>([])
	const COUNT = ref<number>(0)
	const ExclName = ref<string>("")
	function initApplyNow(params:{
		ClassName:string
		Page:string
		PageSize:string
	}){
		return new Promise((resolve,reject)=>{
			getApplyNowList({
				CLASSNAME:params?.ClassName || "",
				PAGEINDEX:params?.Page  || "1",
				PAGESIZE:params?.PageSize || "10",
				OPERATOR_ID:state.user.userInfo.CUSER_ID
			})
			.then(res=>{
				if (res.ERROR[0].errcode == '0'){
					DAT.value = res.DAT
					COUNT.value = parseInt(String(res.COUNT[0].NUM / 10)) + (res.COUNT[0].NUM % 10 === 0 ? 0 : 1)  //分页总页数
				    resolve(res)
				}else {
				    uni.showToast({
				        title: res.ERROR[0].error,
				        icon: 'none',
				        mask: true
				    })
				    reject('err')
				}
			})
			.catch(err=>{
				uni.showToast({
				    title: '获取列表失败',
				    icon: 'error',
				    mask: true
				})
				reject('err')
			})
		})
	}
	function initgetApplyNowListExclName(ClassName:string){
		return new Promise((resolve,reject)=>{
			getApplyNowListExclName({
				CLASSNAME:ClassName,
				OPERATOR_ID:state.user.userInfo.CUSER_ID
			})
			.then(res=>{
				if (res.ERROR[0].errcode == '0'){
					const BASE_URL = import.meta.env.VITE_BASE_API //服务器地址
					ExclName.value =  res.DAT.FILENAME
					uni.downloadFile({
						url: BASE_URL + "/excel_server/browse?" + "excelfilename=" + ExclName.value,
						success(res) {
							const {tempFilePath} = JSON.parse(JSON.stringify(res))  //返回的文件路径
							const wx = uni.getFileSystemManager()   //获取全局文件管理器
							wx.saveFile({    //保存文件到本地
								tempFilePath: tempFilePath,  //配置文件路径
								filePath: uni.env.USER_DATA_PATH + "/专业班报名缴费人数、剩余学位及班级状态.xlsx",  // 文件重命名 可自定义
								success() {
									uni.showToast({
										 title: "导出成功",
										 icon: 'none',
										 mask: true
									})
									setTimeout(()=>{
										uni.openDocument({    //导出成功打开文件
											showMenu: true,
											fileType: 'xlsx',
											filePath: uni.env.USER_DATA_PATH + "/专业班报名缴费人数、剩余学位及班级状态.xlsx", //文件路径
											success: function(res) {
												//打开文件成功
											},
											fail(err) {
											}
										})
									},300)
								}
							})
						}
					})
				    resolve(res)
				}else {
				    uni.showToast({
				        title: res.ERROR[0].error,
				        icon: 'none',
				        mask: true
				    })
				    reject('err')
				}
			})
			.catch(err=>{
				uni.showToast({
				    title: '获取列表失败',
				    icon: 'error',
				    mask: true
				})
				reject('err')
			})
		})
	}
	return {
		initApplyNow,
		DAT,
		COUNT,
		initgetApplyNowListExclName,
		ExclName
	}
}